package dao.impl;

import dao.ProductDao;
import entity.Product;
import entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import java.util.List;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/5 15:32
 */
public class ProductDaoImpl implements ProductDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 查询该类别商品总量
     * @param tid
     * @return
     */
    @Override
    public long findTotalCount(int tid) {
        String sql = "select count(p_id) from product where t_id = ?";
        return template.queryForObject(sql,Long.class,tid);
    }

    /**
     * 查询展示数据集合
     * @param start 从哪条开始
     * @param parseInt 集合的长度
     * @param tid 商品的类别id
     * @return
     */
    @Override
    public List<Product> findProductByPage(int start, int parseInt, int tid) {
        String sql = "select p_id pid,t_id tid,p_name pname,p_time ptime," +
                "p_image pimage,p_price pprice,p_state pstate,p_info pinfo" +
                " from product where t_id =? limit ?,?";
        return template.query(sql, new BeanPropertyRowMapper<Product>(Product.class),tid,start,parseInt);
    }

    /**
     * 根据ID查询商品个体
     * @param pid
     * @return
     */
    @Override
    public Product findProductById(int pid) {
        String sql = "select p_id pid,t_id tid,p_name pname,p_time ptime," +
                "p_image pimage,p_price pprice,p_state pstate,p_info pinfo" +
                " from product where p_id =?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<Product>(Product.class),pid);
    }
}
